package com.hectopascal.goalance.controller;

import com.hectopascal.goalance.pojo.WorkContent;
import com.hectopascal.goalance.pojo.dto.ResponseMessage;
import com.hectopascal.goalance.pojo.dto.WorkContentBack;
import com.hectopascal.goalance.service.Ipml.IWorkContentService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/workcontent")
public class WorkContentController {
    @Autowired
    IWorkContentService workContentService;

    @PostMapping("/addcontent")
    public ResponseMessage<WorkContentBack> workContentAdd(@RequestBody WorkContent workContent, HttpServletRequest request) {
        workContent.setUserId((Integer) request.getAttribute("userId"));
        WorkContentBack workContentBack = workContentService.workContentAdd(workContent);
        return new ResponseMessage<>(200, "添加成功", workContentBack);
    }

    @GetMapping("/getcontents")
    public ResponseMessage<List<WorkContentBack>> workContentGet( @RequestParam(required = false) Integer userId,
                                                                  @RequestParam(required = false) String status,
                                                                  @RequestParam Integer departmentId){
        List<WorkContentBack> workContentBackList = workContentService.workContentGet(userId,status,departmentId);
        return new ResponseMessage<>(200, "获取工作内容成功", workContentBackList);
    }

    @PutMapping("approvecontent")
    public ResponseMessage<String> workContentApprove(@RequestBody WorkContent workContent){
        int rows=workContentService.workContentApprove(workContent);
        if(rows!=0) {
            return new ResponseMessage<>(200, "审批成功", "审批成功");
        }else{
            return new ResponseMessage<>(500, "审批失败", "审批失败");
        }
    }

    @PutMapping("uploadsfile")
    public ResponseMessage<String> uploadsFile(@RequestParam("file") MultipartFile file,@RequestParam Integer id) throws IOException {
        String fileName = workContentService.uploadsFile(file, id);
        return new ResponseMessage<>(200, "上传成功", fileName);
    }
}
